﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VCN.App.DataAccessLayer;
using VCN.App.DataTransferObjects;

namespace VCN.admin.UC
{
    public partial class ViewPartnerUc : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMessage.Visible = false;
            cmdDel.Enabled = false;
            if (!IsPostBack)
                BindData();            
        }

        private void BindData()
        {
            ImageLogoDAO objImagelogoDao = new ImageLogoDAO();
            grvViewPartner.DataSource = objImagelogoDao.selectFilter2List(int.Parse(drdoptionPartner.SelectedValue));
            grvViewPartner.DataBind();
            if (grvViewPartner.Rows.Count == 0)
            {
                cmdDel.Enabled = false;
                lblMessage.Text = "Chưa có logo nào!";
                lblMessage.Visible = true;
            }
            else cmdDel.Enabled = true;
        }

        protected void cmdDel_Click(object sender, EventArgs e)
        {
            int v_i_count = 0;
            foreach (GridViewRow row in grvViewPartner.Rows)
            {
                bool isChecked = ((CheckBox)row.FindControl("chkItem")).Checked;
                if (isChecked)
                {
                    v_i_count += 1;
                    string ID = (((CheckBox)row.FindControl("chkItem")).ToolTip.Trim());
                    ImageLogo v_logo_obj = new ImageLogo();
                    v_logo_obj.Id = int.Parse(ID);
                    ImageLogoDAO v_logo_dao = new ImageLogoDAO();
                    v_logo_dao.deleteByID(v_logo_obj);                    
                }
            }
            // Neu so items duoc check lớn hơn 0
            if (v_i_count > 0)
            {
                // Load lại dữ liêụ
                this.BindData();
                lblMessage.Visible = false;
            }
            // Nếu ko
            else
            {
                lblMessage.Text = "Bạn chưa chọn banner để xóa";
                lblMessage.Visible = true;
            }
        }

        protected string encodeStatus(bool ip_status)
        {
            return (ip_status == true) ? "Hoạt động" : "Không hoạt động";
        }

        protected void grvViewPartner_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                grvViewPartner.PageIndex = e.NewPageIndex;
                this.BindData();
            }
            catch (Exception ip_e)
            {
                m_lbl_message.Visible = true;
                m_lbl_message.Text = ip_e.Message;
            }
        }

        protected void drdoptionPartner_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                BindData();
            }
            catch (Exception ip_e)
            {
                m_lbl_message.Visible = true;
                m_lbl_message.Text = ip_e.Message;
            }
        }

        protected void m_cmd_add_news_Click(object sender, EventArgs e)
        {
            try
            {
                string url = "/admin/addPartner.aspx?new=" + drdoptionPartner.SelectedValue.ToString();
                Response.Redirect(url);
            }
            catch (Exception ip_e)
            {
                m_lbl_message.Visible = true;
                m_lbl_message.Text = ip_e.Message;
            }
        }
    }
}